package com.lishu.carsafe.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.github.mikephil.charting.utils.Utils;
import com.lishu.carsafe.R;
import com.lishu.carsafe.constant.UserPreferences;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.listener.UploadFileListener;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.FaultToAddModel;
import com.lishu.carsafe.model.UploadFileModel;
import com.lishu.carsafe.model.entity.Creator;
import com.lishu.carsafe.model.entity.Level;
import com.lishu.carsafe.model.entity.Type;
import com.lishu.carsafe.model.entity.Vehicle;
import com.lishu.carsafe.model.entity.VehicleId;
import com.lishu.carsafe.utils.ClickUtil;
import com.lishu.carsafe.utils.ImageUtil;
import com.lishu.carsafe.utils.ListUtil;
import com.lishu.carsafe.utils.LogUtil;
import com.lishu.carsafe.utils.NetUtil;
import com.lishu.carsafe.utils.TimeUtil;
import com.lishu.carsafe.utils.ToastUtil;
import com.lishu.carsafe.widget.ChooseDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FaultAddActivity extends FaultEditBaseActivity implements View.OnClickListener{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fault_add);

        initView();
        initEvent();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getFaultOptions();
        //testOptions();
    }

    @Override
    protected void initView() {
        super.initView();
        setTopTitle("故障信息");
        setTopRight(R.drawable.send);
    }

    protected void initEvent() {
        upload_1.setOnClickListener(this);
        upload_2.setOnClickListener(this);
        findViewById(R.id.title_right).setOnClickListener(this);
        findViewById(R.id.fault_car_layout).setOnClickListener(this);
        findViewById(R.id.fault_level_layout).setOnClickListener(this);
        findViewById(R.id.fault_type_layout).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (ClickUtil.isFastClick()) {
            return ;
        }

        switch (view.getId()){
            case R.id.fault_car_layout:
                chooseNumberDialog(1);
                break;
            case R.id.fault_level_layout:
                chooseNumberDialog(2);
                break;
            case R.id.fault_type_layout:
                chooseNumberDialog(3);
                break;
            case R.id.upload_1:
                if(View.VISIBLE == upload_2.getVisibility()) {
                    if("0".equals(mediaType)) {//查看图片
                        Intent intent = new Intent(mContext, PhotoViewActivity.class);
                        intent.putExtra("imagePath", localMediaUrl);
                        startActivity(intent);
                    }else {//播放视频
                        Intent intent = new Intent(mContext, PlayVideoActivity.class);
                        intent.putExtra("videoPath", localMediaUrl);
                        startActivity(intent);
                    }
                }else {
                    getPermissions();
                }
                break;
            case R.id.upload_2:
                getPermissions();
                break;
            case R.id.title_right:
                String title = fault_title.getText().toString();
                String car = fault_car.getText().toString();
                String location = fault_location.getText().toString();
                String level = fault_level.getText().toString();
                String type = fault_type.getText().toString();
                String detail = fault_detail.getText().toString();

                if (!check(title, car, location, level, type, detail)) {
                    return;
                }

                if(TextUtils.isEmpty(localMediaUrl)){
                    ToastUtil.showShort("请添加照片或视频");
                    return;
                }

                sendPicture(new UploadFileListener() {
                    @Override
                    public void uploadFileSuccess(String fileType, String fileUrl) {
                        addFault(fileType, fileUrl);
                    }
                });
                break;
        }
    }

    private void addFault(String mediaType, String uploadedMediaUrl) {
        String title = fault_title.getText().toString();
        //String car = fault_car.getText().toString();
        String location = fault_location.getText().toString();
        //String level = fault_level.getText().toString();
        //String type = fault_type.getText().toString();
        String detail = fault_detail.getText().toString();

        VehicleId vehicleIdEntity = new VehicleId();
        vehicleIdEntity.setId(selectedCar.getId());
        Level levelEntity = new Level();
        levelEntity.setId(selectedLevel.getId());
        Type typeEntity = new Type();
        typeEntity.setId(selectedType.getId());
        Creator creatorEntity = new Creator();
        creatorEntity.setId(UserPreferences.getInstance().getUserId());
        String time = TimeUtil.getCurDatetime("yyyy-MM-dd HH:mm:ss");

        showAlways("正在处理，请稍候...");
        HttpLoader.faultAdd(title, vehicleIdEntity, location, levelEntity, typeEntity, detail,
                creatorEntity, time, mediaType, uploadedMediaUrl, new HttpBase.IResponseListener() {
                    @Override
                    public void onResponse(BaseModel model) {
                        hide();
                        if (model == null) {
                            ToastUtil.showShort(R.string.please_check_network);
                            return;
                        }
                        if (!model.success()) {
                            ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                            return;
                        }

                        ToastUtil.showShort("上报成功！");
                        finish();
                    }
                });
    }
}
